{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:16.487188Z",
     "start_time": "2021-08-03T01:47:15.635461Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import talib as ta\n",
    "from tqdm import tqdm\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:17.634237Z",
     "start_time": "2021-08-03T01:47:16.537274Z"
    }
   },
   "outputs": [],
   "source": [
    "#个人写的脚本文件\n",
    "#更新数据库信息\n",
    "from update_sql import update_sql,info_sql\n",
    "#画K线图\n",
    "from plot_stock import stock_plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-26T01:16:08.301827Z",
     "start_time": "2021-05-26T01:15:49.918427Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据库已更新至20210525日数据\n",
      "统计查询的总数：9235701\n",
      "数据期间：20050104——20210525\n",
      "数据库包含股票个数：4346\n"
     ]
    }
   ],
   "source": [
    "#更新数据库\n",
    "update_sql()\n",
    "#获取数据库信息\n",
    "info_sql()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:19.714838Z",
     "start_time": "2021-08-03T01:47:19.704843Z"
    }
   },
   "outputs": [],
   "source": [
    "from base import sql_engine,ts_pro\n",
    "from datetime import datetime,timedelta\n",
    "pro=ts_pro()\n",
    "engine = sql_engine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:20.465270Z",
     "start_time": "2021-08-03T01:47:20.457267Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_code_list(d=2018):\n",
    "    df=pro.stock_basic()\n",
    "    df=df[df.list_date<str(d)]\n",
    "    df=df[-(df.name.str.startswith('ST') | df.name.str.startswith('*ST'))]\n",
    "    codes=df.ts_code.values\n",
    "    names=df.name.values\n",
    "    return codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:20.918437Z",
     "start_time": "2021-08-03T01:47:20.913438Z"
    }
   },
   "outputs": [],
   "source": [
    "#常用大盘指数\n",
    "indexs={'上证综指': '000001.SH','创业板指': '399006.SZ','中小板指': '399005.SZ'}\n",
    "def get_index_ret(code):\n",
    "    df=pro.index_daily(ts_code=code)\n",
    "    df.index=pd.to_datetime(df.trade_date)\n",
    "    df=(df.sort_index()).drop('trade_date',axis=1)\n",
    "    return df.close.pct_change(250)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T01:47:22.228656Z",
     "start_time": "2021-08-03T01:47:22.209651Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_all_data(d=2019):\n",
    "    sql=f'select * from daily_data where trade_date>{str(d)}'\n",
    "    all_data=pd.read_sql(sql,engine)\n",
    "    all_data=all_data.sort_values(['ts_code','trade_date'])\n",
    "    all_data=all_data.drop_duplicates()\n",
    "    codes=list(all_data.ts_code.unique())\n",
    "    #前复权\n",
    "    all_data['adjclose']=all_data.groupby('ts_code').apply(lambda x:x.close*x.adj_factor/x.adj_factor.iloc[-1]).values\n",
    "    #设置索引\n",
    "    all_data=all_data.set_index(['trade_date','ts_code'])['adjclose']\n",
    "    all_data=all_data.unstack()\n",
    "    #排除st股、次新股\n",
    "    all_data=all_data[get_code_list()]\n",
    "    #排除观察值少于300个交易日个股\n",
    "    final_data=all_data[-300:].dropna(axis=1)\n",
    "    return final_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:20:25.884436Z",
     "start_time": "2021-08-03T06:20:25.868427Z"
    }
   },
   "outputs": [],
   "source": [
    "def select_stock(close: pd.Series, index_ret: str) -> pd.Series:\n",
    "    \"\"\"close(pd.Series): 股票收盘价\n",
    "       index_code(str): 基准指数代码\n",
    "    \"\"\"\n",
    "    # 计算50，150，200日均线\n",
    "    ma_50 = ta.EMA(close, 20)\n",
    "    ma_150 = ta.EMA(close, 120)\n",
    "    ma_200 = ta.EMA(close, 200)\n",
    "\n",
    "    # 200日均线的20日移动平滑，用于判断200日均线是否上升\n",
    "    ma_200_smooth = ta.EMA(ma_200, 20)\n",
    "    \n",
    "    up200=ta.LINEARREG_SLOPE(ma_200)[-1]\n",
    "\n",
    "    # 收盘价的52周高点和52周低点\n",
    "    w_52_high = close[-52*5:-1].max()\n",
    "    w_52_low = close[-52*5:-1].min()\n",
    "    \n",
    "    ret_stock=close.pct_change(250).iloc[-1]\n",
    "    rs= (ret_stock-index_ret)\n",
    "    #条件筛选\n",
    "    c1=close[-1]>ma_50[-1]>ma_150[-1]>ma_200[-1]\n",
    "    c2=w_52_low*1.3<close[-1]\n",
    "    c3=w_52_high*0.75<close[-1]<w_52_high*1.20\n",
    "    c4=up200>0\n",
    "    c5=rs>0.7\n",
    "    c=c1&c2&c3&c4&c5\n",
    "    flag=True if c else False\n",
    "    \n",
    "    result = {\n",
    "        \"收盘价\": close[-1],\n",
    "        \"50日均线\": ma_50[-1],\n",
    "        \"150日均线\": ma_150[-1],\n",
    "        \"200日均线\": ma_200[-1],\n",
    "        \"52周最高价\": w_52_high,\n",
    "        \"52周最低价\": w_52_low,\n",
    "        \"相对强弱\": round(rs, 2),\n",
    "        \"满足条件\": flag\n",
    "    }\n",
    "\n",
    "    return pd.Series(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:20:52.624429Z",
     "start_time": "2021-08-03T06:20:26.348533Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ts_code</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000004.SZ</th>\n",
       "      <th>000006.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000011.SZ</th>\n",
       "      <th>000012.SZ</th>\n",
       "      <th>000014.SZ</th>\n",
       "      <th>000016.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>603980.SH</th>\n",
       "      <th>603986.SH</th>\n",
       "      <th>603987.SH</th>\n",
       "      <th>603989.SH</th>\n",
       "      <th>603990.SH</th>\n",
       "      <th>603991.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "      <th>603997.SH</th>\n",
       "      <th>603998.SH</th>\n",
       "      <th>603999.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-05-13</th>\n",
       "      <td>13.294855</td>\n",
       "      <td>25.005186</td>\n",
       "      <td>33.55</td>\n",
       "      <td>4.601508</td>\n",
       "      <td>3.089624</td>\n",
       "      <td>7.341768</td>\n",
       "      <td>8.500790</td>\n",
       "      <td>4.558140</td>\n",
       "      <td>8.409833</td>\n",
       "      <td>7.433069</td>\n",
       "      <td>...</td>\n",
       "      <td>6.442706</td>\n",
       "      <td>155.939029</td>\n",
       "      <td>14.646043</td>\n",
       "      <td>27.694021</td>\n",
       "      <td>35.391088</td>\n",
       "      <td>26.90</td>\n",
       "      <td>3.492809</td>\n",
       "      <td>7.43</td>\n",
       "      <td>6.359764</td>\n",
       "      <td>5.731985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-14</th>\n",
       "      <td>12.972969</td>\n",
       "      <td>24.580879</td>\n",
       "      <td>33.09</td>\n",
       "      <td>4.592155</td>\n",
       "      <td>3.059725</td>\n",
       "      <td>7.102362</td>\n",
       "      <td>8.463587</td>\n",
       "      <td>4.470295</td>\n",
       "      <td>8.479665</td>\n",
       "      <td>7.384167</td>\n",
       "      <td>...</td>\n",
       "      <td>6.387718</td>\n",
       "      <td>152.898574</td>\n",
       "      <td>14.497603</td>\n",
       "      <td>27.459077</td>\n",
       "      <td>35.452305</td>\n",
       "      <td>27.69</td>\n",
       "      <td>3.414098</td>\n",
       "      <td>7.27</td>\n",
       "      <td>6.223622</td>\n",
       "      <td>5.781399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-15</th>\n",
       "      <td>12.904691</td>\n",
       "      <td>24.580879</td>\n",
       "      <td>31.38</td>\n",
       "      <td>4.610861</td>\n",
       "      <td>3.049758</td>\n",
       "      <td>7.222065</td>\n",
       "      <td>8.510090</td>\n",
       "      <td>4.460535</td>\n",
       "      <td>8.499617</td>\n",
       "      <td>7.941647</td>\n",
       "      <td>...</td>\n",
       "      <td>6.387718</td>\n",
       "      <td>163.771583</td>\n",
       "      <td>14.081972</td>\n",
       "      <td>27.302447</td>\n",
       "      <td>37.503075</td>\n",
       "      <td>26.88</td>\n",
       "      <td>3.423937</td>\n",
       "      <td>7.06</td>\n",
       "      <td>6.223622</td>\n",
       "      <td>5.781399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-18</th>\n",
       "      <td>12.875428</td>\n",
       "      <td>25.275200</td>\n",
       "      <td>29.89</td>\n",
       "      <td>4.536039</td>\n",
       "      <td>3.059725</td>\n",
       "      <td>6.882907</td>\n",
       "      <td>8.530354</td>\n",
       "      <td>4.509337</td>\n",
       "      <td>8.280144</td>\n",
       "      <td>7.384167</td>\n",
       "      <td>...</td>\n",
       "      <td>6.314401</td>\n",
       "      <td>153.802878</td>\n",
       "      <td>14.042388</td>\n",
       "      <td>27.145818</td>\n",
       "      <td>36.500646</td>\n",
       "      <td>26.29</td>\n",
       "      <td>3.502648</td>\n",
       "      <td>7.03</td>\n",
       "      <td>6.223622</td>\n",
       "      <td>5.692455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-19</th>\n",
       "      <td>13.031494</td>\n",
       "      <td>25.198053</td>\n",
       "      <td>30.14</td>\n",
       "      <td>4.526687</td>\n",
       "      <td>3.079658</td>\n",
       "      <td>6.912833</td>\n",
       "      <td>8.481941</td>\n",
       "      <td>4.577661</td>\n",
       "      <td>8.320048</td>\n",
       "      <td>7.579774</td>\n",
       "      <td>...</td>\n",
       "      <td>6.341895</td>\n",
       "      <td>158.018216</td>\n",
       "      <td>14.368955</td>\n",
       "      <td>27.831071</td>\n",
       "      <td>36.163953</td>\n",
       "      <td>26.53</td>\n",
       "      <td>3.492809</td>\n",
       "      <td>6.94</td>\n",
       "      <td>6.291693</td>\n",
       "      <td>5.712220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-27</th>\n",
       "      <td>17.760000</td>\n",
       "      <td>20.160000</td>\n",
       "      <td>22.69</td>\n",
       "      <td>4.500000</td>\n",
       "      <td>2.100000</td>\n",
       "      <td>21.410000</td>\n",
       "      <td>11.680000</td>\n",
       "      <td>9.870000</td>\n",
       "      <td>7.030000</td>\n",
       "      <td>6.180000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.690000</td>\n",
       "      <td>205.620000</td>\n",
       "      <td>19.460000</td>\n",
       "      <td>33.700000</td>\n",
       "      <td>23.180000</td>\n",
       "      <td>44.03</td>\n",
       "      <td>6.920000</td>\n",
       "      <td>9.01</td>\n",
       "      <td>5.460000</td>\n",
       "      <td>4.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-28</th>\n",
       "      <td>17.750000</td>\n",
       "      <td>20.490000</td>\n",
       "      <td>20.80</td>\n",
       "      <td>4.420000</td>\n",
       "      <td>2.030000</td>\n",
       "      <td>19.270000</td>\n",
       "      <td>11.710000</td>\n",
       "      <td>9.180000</td>\n",
       "      <td>6.840000</td>\n",
       "      <td>5.890000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.560000</td>\n",
       "      <td>201.890000</td>\n",
       "      <td>18.940000</td>\n",
       "      <td>32.100000</td>\n",
       "      <td>23.910000</td>\n",
       "      <td>43.02</td>\n",
       "      <td>6.480000</td>\n",
       "      <td>8.82</td>\n",
       "      <td>5.360000</td>\n",
       "      <td>4.790000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-29</th>\n",
       "      <td>17.960000</td>\n",
       "      <td>20.600000</td>\n",
       "      <td>21.40</td>\n",
       "      <td>4.430000</td>\n",
       "      <td>2.050000</td>\n",
       "      <td>21.200000</td>\n",
       "      <td>11.740000</td>\n",
       "      <td>9.480000</td>\n",
       "      <td>6.830000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.630000</td>\n",
       "      <td>214.910000</td>\n",
       "      <td>20.530000</td>\n",
       "      <td>33.790000</td>\n",
       "      <td>25.240000</td>\n",
       "      <td>43.00</td>\n",
       "      <td>6.950000</td>\n",
       "      <td>9.08</td>\n",
       "      <td>5.420000</td>\n",
       "      <td>4.870000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-30</th>\n",
       "      <td>17.690000</td>\n",
       "      <td>20.660000</td>\n",
       "      <td>21.99</td>\n",
       "      <td>4.420000</td>\n",
       "      <td>2.040000</td>\n",
       "      <td>21.220000</td>\n",
       "      <td>11.580000</td>\n",
       "      <td>9.700000</td>\n",
       "      <td>6.740000</td>\n",
       "      <td>5.960000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.630000</td>\n",
       "      <td>230.050000</td>\n",
       "      <td>21.100000</td>\n",
       "      <td>34.900000</td>\n",
       "      <td>24.420000</td>\n",
       "      <td>47.30</td>\n",
       "      <td>6.910000</td>\n",
       "      <td>8.92</td>\n",
       "      <td>5.500000</td>\n",
       "      <td>4.840000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-02</th>\n",
       "      <td>18.010000</td>\n",
       "      <td>20.900000</td>\n",
       "      <td>22.70</td>\n",
       "      <td>4.460000</td>\n",
       "      <td>2.070000</td>\n",
       "      <td>22.260000</td>\n",
       "      <td>11.710000</td>\n",
       "      <td>10.210000</td>\n",
       "      <td>6.860000</td>\n",
       "      <td>6.010000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.690000</td>\n",
       "      <td>220.000000</td>\n",
       "      <td>21.170000</td>\n",
       "      <td>35.010000</td>\n",
       "      <td>25.410000</td>\n",
       "      <td>46.22</td>\n",
       "      <td>6.850000</td>\n",
       "      <td>9.35</td>\n",
       "      <td>5.530000</td>\n",
       "      <td>4.890000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>300 rows × 2855 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ts_code     000001.SZ  000002.SZ  000004.SZ  000006.SZ  000008.SZ  000009.SZ  \\\n",
       "trade_date                                                                     \n",
       "2020-05-13  13.294855  25.005186      33.55   4.601508   3.089624   7.341768   \n",
       "2020-05-14  12.972969  24.580879      33.09   4.592155   3.059725   7.102362   \n",
       "2020-05-15  12.904691  24.580879      31.38   4.610861   3.049758   7.222065   \n",
       "2020-05-18  12.875428  25.275200      29.89   4.536039   3.059725   6.882907   \n",
       "2020-05-19  13.031494  25.198053      30.14   4.526687   3.079658   6.912833   \n",
       "...               ...        ...        ...        ...        ...        ...   \n",
       "2021-07-27  17.760000  20.160000      22.69   4.500000   2.100000  21.410000   \n",
       "2021-07-28  17.750000  20.490000      20.80   4.420000   2.030000  19.270000   \n",
       "2021-07-29  17.960000  20.600000      21.40   4.430000   2.050000  21.200000   \n",
       "2021-07-30  17.690000  20.660000      21.99   4.420000   2.040000  21.220000   \n",
       "2021-08-02  18.010000  20.900000      22.70   4.460000   2.070000  22.260000   \n",
       "\n",
       "ts_code     000011.SZ  000012.SZ  000014.SZ  000016.SZ  ...  603980.SH  \\\n",
       "trade_date                                              ...              \n",
       "2020-05-13   8.500790   4.558140   8.409833   7.433069  ...   6.442706   \n",
       "2020-05-14   8.463587   4.470295   8.479665   7.384167  ...   6.387718   \n",
       "2020-05-15   8.510090   4.460535   8.499617   7.941647  ...   6.387718   \n",
       "2020-05-18   8.530354   4.509337   8.280144   7.384167  ...   6.314401   \n",
       "2020-05-19   8.481941   4.577661   8.320048   7.579774  ...   6.341895   \n",
       "...               ...        ...        ...        ...  ...        ...   \n",
       "2021-07-27  11.680000   9.870000   7.030000   6.180000  ...   6.690000   \n",
       "2021-07-28  11.710000   9.180000   6.840000   5.890000  ...   6.560000   \n",
       "2021-07-29  11.740000   9.480000   6.830000   6.000000  ...   6.630000   \n",
       "2021-07-30  11.580000   9.700000   6.740000   5.960000  ...   6.630000   \n",
       "2021-08-02  11.710000  10.210000   6.860000   6.010000  ...   6.690000   \n",
       "\n",
       "ts_code      603986.SH  603987.SH  603989.SH  603990.SH  603991.SH  603993.SH  \\\n",
       "trade_date                                                                      \n",
       "2020-05-13  155.939029  14.646043  27.694021  35.391088      26.90   3.492809   \n",
       "2020-05-14  152.898574  14.497603  27.459077  35.452305      27.69   3.414098   \n",
       "2020-05-15  163.771583  14.081972  27.302447  37.503075      26.88   3.423937   \n",
       "2020-05-18  153.802878  14.042388  27.145818  36.500646      26.29   3.502648   \n",
       "2020-05-19  158.018216  14.368955  27.831071  36.163953      26.53   3.492809   \n",
       "...                ...        ...        ...        ...        ...        ...   \n",
       "2021-07-27  205.620000  19.460000  33.700000  23.180000      44.03   6.920000   \n",
       "2021-07-28  201.890000  18.940000  32.100000  23.910000      43.02   6.480000   \n",
       "2021-07-29  214.910000  20.530000  33.790000  25.240000      43.00   6.950000   \n",
       "2021-07-30  230.050000  21.100000  34.900000  24.420000      47.30   6.910000   \n",
       "2021-08-02  220.000000  21.170000  35.010000  25.410000      46.22   6.850000   \n",
       "\n",
       "ts_code     603997.SH  603998.SH  603999.SH  \n",
       "trade_date                                   \n",
       "2020-05-13       7.43   6.359764   5.731985  \n",
       "2020-05-14       7.27   6.223622   5.781399  \n",
       "2020-05-15       7.06   6.223622   5.781399  \n",
       "2020-05-18       7.03   6.223622   5.692455  \n",
       "2020-05-19       6.94   6.291693   5.712220  \n",
       "...               ...        ...        ...  \n",
       "2021-07-27       9.01   5.460000   4.900000  \n",
       "2021-07-28       8.82   5.360000   4.790000  \n",
       "2021-07-29       9.08   5.420000   4.870000  \n",
       "2021-07-30       8.92   5.500000   4.840000  \n",
       "2021-08-02       9.35   5.530000   4.890000  \n",
       "\n",
       "[300 rows x 2855 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=get_all_data()\n",
    "df.index=pd.to_datetime(df.index)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:20:52.656577Z",
     "start_time": "2021-08-03T06:20:52.629430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ts_code</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000004.SZ</th>\n",
       "      <th>000006.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000011.SZ</th>\n",
       "      <th>000012.SZ</th>\n",
       "      <th>000014.SZ</th>\n",
       "      <th>000016.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>603980.SH</th>\n",
       "      <th>603986.SH</th>\n",
       "      <th>603987.SH</th>\n",
       "      <th>603989.SH</th>\n",
       "      <th>603990.SH</th>\n",
       "      <th>603991.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "      <th>603997.SH</th>\n",
       "      <th>603998.SH</th>\n",
       "      <th>603999.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-05-17</th>\n",
       "      <td>12.904691</td>\n",
       "      <td>24.580879</td>\n",
       "      <td>31.38</td>\n",
       "      <td>4.610861</td>\n",
       "      <td>3.049758</td>\n",
       "      <td>7.222065</td>\n",
       "      <td>8.510090</td>\n",
       "      <td>4.460535</td>\n",
       "      <td>8.499617</td>\n",
       "      <td>7.941647</td>\n",
       "      <td>...</td>\n",
       "      <td>6.387718</td>\n",
       "      <td>163.771583</td>\n",
       "      <td>14.081972</td>\n",
       "      <td>27.302447</td>\n",
       "      <td>37.503075</td>\n",
       "      <td>26.88</td>\n",
       "      <td>3.423937</td>\n",
       "      <td>7.06</td>\n",
       "      <td>6.223622</td>\n",
       "      <td>5.781399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-24</th>\n",
       "      <td>12.602313</td>\n",
       "      <td>24.262649</td>\n",
       "      <td>28.11</td>\n",
       "      <td>4.377044</td>\n",
       "      <td>2.950093</td>\n",
       "      <td>6.533774</td>\n",
       "      <td>8.026859</td>\n",
       "      <td>4.411733</td>\n",
       "      <td>8.180383</td>\n",
       "      <td>6.885369</td>\n",
       "      <td>...</td>\n",
       "      <td>6.268579</td>\n",
       "      <td>145.970324</td>\n",
       "      <td>13.550000</td>\n",
       "      <td>26.724877</td>\n",
       "      <td>34.266225</td>\n",
       "      <td>24.20</td>\n",
       "      <td>3.364903</td>\n",
       "      <td>6.72</td>\n",
       "      <td>6.097205</td>\n",
       "      <td>5.504683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-31</th>\n",
       "      <td>12.898598</td>\n",
       "      <td>24.783389</td>\n",
       "      <td>28.88</td>\n",
       "      <td>4.498629</td>\n",
       "      <td>2.990000</td>\n",
       "      <td>6.882907</td>\n",
       "      <td>9.188769</td>\n",
       "      <td>4.441014</td>\n",
       "      <td>8.639282</td>\n",
       "      <td>6.425692</td>\n",
       "      <td>...</td>\n",
       "      <td>6.369389</td>\n",
       "      <td>137.703419</td>\n",
       "      <td>15.700000</td>\n",
       "      <td>26.509511</td>\n",
       "      <td>35.987954</td>\n",
       "      <td>27.23</td>\n",
       "      <td>3.364903</td>\n",
       "      <td>6.92</td>\n",
       "      <td>6.204173</td>\n",
       "      <td>5.583744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-06-07</th>\n",
       "      <td>13.483996</td>\n",
       "      <td>25.468067</td>\n",
       "      <td>29.24</td>\n",
       "      <td>4.751151</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>7.232040</td>\n",
       "      <td>10.931634</td>\n",
       "      <td>4.538619</td>\n",
       "      <td>9.158038</td>\n",
       "      <td>6.953831</td>\n",
       "      <td>...</td>\n",
       "      <td>6.561845</td>\n",
       "      <td>161.087153</td>\n",
       "      <td>15.170000</td>\n",
       "      <td>26.626983</td>\n",
       "      <td>33.133710</td>\n",
       "      <td>28.50</td>\n",
       "      <td>3.551842</td>\n",
       "      <td>7.07</td>\n",
       "      <td>6.320866</td>\n",
       "      <td>5.880226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-06-14</th>\n",
       "      <td>12.888676</td>\n",
       "      <td>24.793033</td>\n",
       "      <td>28.46</td>\n",
       "      <td>4.956909</td>\n",
       "      <td>2.960000</td>\n",
       "      <td>7.172189</td>\n",
       "      <td>13.962281</td>\n",
       "      <td>4.626463</td>\n",
       "      <td>9.577646</td>\n",
       "      <td>6.807126</td>\n",
       "      <td>...</td>\n",
       "      <td>6.396883</td>\n",
       "      <td>163.792944</td>\n",
       "      <td>14.120000</td>\n",
       "      <td>26.440986</td>\n",
       "      <td>32.766408</td>\n",
       "      <td>28.68</td>\n",
       "      <td>3.512487</td>\n",
       "      <td>6.84</td>\n",
       "      <td>6.204173</td>\n",
       "      <td>6.087764</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-11</th>\n",
       "      <td>21.270000</td>\n",
       "      <td>24.060000</td>\n",
       "      <td>18.71</td>\n",
       "      <td>4.800000</td>\n",
       "      <td>2.180000</td>\n",
       "      <td>21.980000</td>\n",
       "      <td>11.760000</td>\n",
       "      <td>9.800000</td>\n",
       "      <td>7.590000</td>\n",
       "      <td>6.230000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.510000</td>\n",
       "      <td>189.000000</td>\n",
       "      <td>22.530000</td>\n",
       "      <td>32.700000</td>\n",
       "      <td>23.460000</td>\n",
       "      <td>48.96</td>\n",
       "      <td>6.650000</td>\n",
       "      <td>8.18</td>\n",
       "      <td>5.950000</td>\n",
       "      <td>5.150000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-18</th>\n",
       "      <td>21.340000</td>\n",
       "      <td>23.950000</td>\n",
       "      <td>23.31</td>\n",
       "      <td>4.770000</td>\n",
       "      <td>2.170000</td>\n",
       "      <td>21.950000</td>\n",
       "      <td>12.050000</td>\n",
       "      <td>11.710000</td>\n",
       "      <td>7.610000</td>\n",
       "      <td>6.150000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.740000</td>\n",
       "      <td>167.550000</td>\n",
       "      <td>23.830000</td>\n",
       "      <td>33.740000</td>\n",
       "      <td>24.740000</td>\n",
       "      <td>44.25</td>\n",
       "      <td>6.730000</td>\n",
       "      <td>9.33</td>\n",
       "      <td>5.610000</td>\n",
       "      <td>5.150000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-25</th>\n",
       "      <td>20.100000</td>\n",
       "      <td>23.110000</td>\n",
       "      <td>24.26</td>\n",
       "      <td>4.700000</td>\n",
       "      <td>2.170000</td>\n",
       "      <td>23.180000</td>\n",
       "      <td>12.290000</td>\n",
       "      <td>11.180000</td>\n",
       "      <td>7.360000</td>\n",
       "      <td>6.230000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.790000</td>\n",
       "      <td>196.010000</td>\n",
       "      <td>21.290000</td>\n",
       "      <td>36.460000</td>\n",
       "      <td>25.840000</td>\n",
       "      <td>44.18</td>\n",
       "      <td>7.060000</td>\n",
       "      <td>9.28</td>\n",
       "      <td>5.670000</td>\n",
       "      <td>5.030000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-01</th>\n",
       "      <td>17.690000</td>\n",
       "      <td>20.660000</td>\n",
       "      <td>21.99</td>\n",
       "      <td>4.420000</td>\n",
       "      <td>2.040000</td>\n",
       "      <td>21.220000</td>\n",
       "      <td>11.580000</td>\n",
       "      <td>9.700000</td>\n",
       "      <td>6.740000</td>\n",
       "      <td>5.960000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.630000</td>\n",
       "      <td>230.050000</td>\n",
       "      <td>21.100000</td>\n",
       "      <td>34.900000</td>\n",
       "      <td>24.420000</td>\n",
       "      <td>47.30</td>\n",
       "      <td>6.910000</td>\n",
       "      <td>8.92</td>\n",
       "      <td>5.500000</td>\n",
       "      <td>4.840000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-08</th>\n",
       "      <td>18.010000</td>\n",
       "      <td>20.900000</td>\n",
       "      <td>22.70</td>\n",
       "      <td>4.460000</td>\n",
       "      <td>2.070000</td>\n",
       "      <td>22.260000</td>\n",
       "      <td>11.710000</td>\n",
       "      <td>10.210000</td>\n",
       "      <td>6.860000</td>\n",
       "      <td>6.010000</td>\n",
       "      <td>...</td>\n",
       "      <td>6.690000</td>\n",
       "      <td>220.000000</td>\n",
       "      <td>21.170000</td>\n",
       "      <td>35.010000</td>\n",
       "      <td>25.410000</td>\n",
       "      <td>46.22</td>\n",
       "      <td>6.850000</td>\n",
       "      <td>9.35</td>\n",
       "      <td>5.530000</td>\n",
       "      <td>4.890000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>65 rows × 2855 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ts_code     000001.SZ  000002.SZ  000004.SZ  000006.SZ  000008.SZ  000009.SZ  \\\n",
       "trade_date                                                                     \n",
       "2020-05-17  12.904691  24.580879      31.38   4.610861   3.049758   7.222065   \n",
       "2020-05-24  12.602313  24.262649      28.11   4.377044   2.950093   6.533774   \n",
       "2020-05-31  12.898598  24.783389      28.88   4.498629   2.990000   6.882907   \n",
       "2020-06-07  13.483996  25.468067      29.24   4.751151   3.000000   7.232040   \n",
       "2020-06-14  12.888676  24.793033      28.46   4.956909   2.960000   7.172189   \n",
       "...               ...        ...        ...        ...        ...        ...   \n",
       "2021-07-11  21.270000  24.060000      18.71   4.800000   2.180000  21.980000   \n",
       "2021-07-18  21.340000  23.950000      23.31   4.770000   2.170000  21.950000   \n",
       "2021-07-25  20.100000  23.110000      24.26   4.700000   2.170000  23.180000   \n",
       "2021-08-01  17.690000  20.660000      21.99   4.420000   2.040000  21.220000   \n",
       "2021-08-08  18.010000  20.900000      22.70   4.460000   2.070000  22.260000   \n",
       "\n",
       "ts_code     000011.SZ  000012.SZ  000014.SZ  000016.SZ  ...  603980.SH  \\\n",
       "trade_date                                              ...              \n",
       "2020-05-17   8.510090   4.460535   8.499617   7.941647  ...   6.387718   \n",
       "2020-05-24   8.026859   4.411733   8.180383   6.885369  ...   6.268579   \n",
       "2020-05-31   9.188769   4.441014   8.639282   6.425692  ...   6.369389   \n",
       "2020-06-07  10.931634   4.538619   9.158038   6.953831  ...   6.561845   \n",
       "2020-06-14  13.962281   4.626463   9.577646   6.807126  ...   6.396883   \n",
       "...               ...        ...        ...        ...  ...        ...   \n",
       "2021-07-11  11.760000   9.800000   7.590000   6.230000  ...   6.510000   \n",
       "2021-07-18  12.050000  11.710000   7.610000   6.150000  ...   6.740000   \n",
       "2021-07-25  12.290000  11.180000   7.360000   6.230000  ...   6.790000   \n",
       "2021-08-01  11.580000   9.700000   6.740000   5.960000  ...   6.630000   \n",
       "2021-08-08  11.710000  10.210000   6.860000   6.010000  ...   6.690000   \n",
       "\n",
       "ts_code      603986.SH  603987.SH  603989.SH  603990.SH  603991.SH  603993.SH  \\\n",
       "trade_date                                                                      \n",
       "2020-05-17  163.771583  14.081972  27.302447  37.503075      26.88   3.423937   \n",
       "2020-05-24  145.970324  13.550000  26.724877  34.266225      24.20   3.364903   \n",
       "2020-05-31  137.703419  15.700000  26.509511  35.987954      27.23   3.364903   \n",
       "2020-06-07  161.087153  15.170000  26.626983  33.133710      28.50   3.551842   \n",
       "2020-06-14  163.792944  14.120000  26.440986  32.766408      28.68   3.512487   \n",
       "...                ...        ...        ...        ...        ...        ...   \n",
       "2021-07-11  189.000000  22.530000  32.700000  23.460000      48.96   6.650000   \n",
       "2021-07-18  167.550000  23.830000  33.740000  24.740000      44.25   6.730000   \n",
       "2021-07-25  196.010000  21.290000  36.460000  25.840000      44.18   7.060000   \n",
       "2021-08-01  230.050000  21.100000  34.900000  24.420000      47.30   6.910000   \n",
       "2021-08-08  220.000000  21.170000  35.010000  25.410000      46.22   6.850000   \n",
       "\n",
       "ts_code     603997.SH  603998.SH  603999.SH  \n",
       "trade_date                                   \n",
       "2020-05-17       7.06   6.223622   5.781399  \n",
       "2020-05-24       6.72   6.097205   5.504683  \n",
       "2020-05-31       6.92   6.204173   5.583744  \n",
       "2020-06-07       7.07   6.320866   5.880226  \n",
       "2020-06-14       6.84   6.204173   6.087764  \n",
       "...               ...        ...        ...  \n",
       "2021-07-11       8.18   5.950000   5.150000  \n",
       "2021-07-18       9.33   5.610000   5.150000  \n",
       "2021-07-25       9.28   5.670000   5.030000  \n",
       "2021-08-01       8.92   5.500000   4.840000  \n",
       "2021-08-08       9.35   5.530000   4.890000  \n",
       "\n",
       "[65 rows x 2855 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=df.resample('W').last()\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:22:58.450491Z",
     "start_time": "2021-08-03T06:22:58.444501Z"
    }
   },
   "outputs": [],
   "source": [
    "def select_stock2(close: pd.Series, index_ret: str) -> pd.Series:\n",
    "    \"\"\"close(pd.Series): 股票收盘价\n",
    "       index_code(str): 基准指数代码\n",
    "    \"\"\"\n",
    "    # 计算50，150，200日均线\n",
    "    ma_5 = ta.EMA(close, 5)\n",
    "    ma_20 = ta.EMA(close, 20)\n",
    "\n",
    "    # 200日均线的20日移动平滑，用于判断200日均线是否上升\n",
    "    up20=ta.LINEARREG_SLOPE(ma_20)[-1]\n",
    "\n",
    "    # 收盘价的52周高点和52周低点\n",
    "    w_52_high = close[-52:-1].max()\n",
    "    w_52_low = close[-52:-1].min()\n",
    "    \n",
    "    ret_stock=close.pct_change(52).iloc[-1]\n",
    "    rs= (ret_stock-index_ret)\n",
    "    #条件筛选\n",
    "    c1=close[-1]>ma_5[-1]>ma_20[-1]\n",
    "    c2=w_52_low*1.3<close[-1]\n",
    "    c3=w_52_high*0.75<close[-1]<w_52_high*1.20\n",
    "    c4=up20>0\n",
    "    c5=rs>0.7\n",
    "    c=c1&c2&c3&c4&c5\n",
    "    flag=True if c else False\n",
    "    \n",
    "    result = {\n",
    "        \"收盘价\": close[-1],\n",
    "        \"5周均线\": ma_5[-1],\n",
    "        \"20周均线\": ma_20[-1],\n",
    "        \"52周最高价\": w_52_high,\n",
    "        \"52周最低价\": w_52_low,\n",
    "        \"相对强弱\": round(rs, 2),\n",
    "        \"满足条件\": flag\n",
    "    }\n",
    "\n",
    "    return pd.Series(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:23:01.338032Z",
     "start_time": "2021-08-03T06:22:58.987493Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘价</th>\n",
       "      <th>5周均线</th>\n",
       "      <th>20周均线</th>\n",
       "      <th>52周最高价</th>\n",
       "      <th>52周最低价</th>\n",
       "      <th>相对强弱</th>\n",
       "      <th>满足条件</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000001.SZ</th>\n",
       "      <td>18.01</td>\n",
       "      <td>18.1428</td>\n",
       "      <td>19.8788</td>\n",
       "      <td>25.01</td>\n",
       "      <td>17.69</td>\n",
       "      <td>-0.44</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.SZ</th>\n",
       "      <td>20.9</td>\n",
       "      <td>20.9335</td>\n",
       "      <td>22.3858</td>\n",
       "      <td>27.18</td>\n",
       "      <td>20.16</td>\n",
       "      <td>-0.42</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000004.SZ</th>\n",
       "      <td>22.7</td>\n",
       "      <td>22.3045</td>\n",
       "      <td>22.1178</td>\n",
       "      <td>25.97</td>\n",
       "      <td>15.09</td>\n",
       "      <td>0.3</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.SZ</th>\n",
       "      <td>4.46</td>\n",
       "      <td>4.47137</td>\n",
       "      <td>4.65279</td>\n",
       "      <td>5.1178</td>\n",
       "      <td>4.42</td>\n",
       "      <td>-0.34</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000008.SZ</th>\n",
       "      <td>2.07</td>\n",
       "      <td>2.07017</td>\n",
       "      <td>2.13248</td>\n",
       "      <td>2.29</td>\n",
       "      <td>2.03</td>\n",
       "      <td>-0.28</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603991.SH</th>\n",
       "      <td>46.22</td>\n",
       "      <td>45.2895</td>\n",
       "      <td>45.9498</td>\n",
       "      <td>56.33</td>\n",
       "      <td>42.52</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603993.SH</th>\n",
       "      <td>6.85</td>\n",
       "      <td>6.8712</td>\n",
       "      <td>6.60969</td>\n",
       "      <td>7.29</td>\n",
       "      <td>5.02337</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603997.SH</th>\n",
       "      <td>9.35</td>\n",
       "      <td>9.12559</td>\n",
       "      <td>9.00023</td>\n",
       "      <td>9.64</td>\n",
       "      <td>8.01</td>\n",
       "      <td>-0.08</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603998.SH</th>\n",
       "      <td>5.53</td>\n",
       "      <td>5.50385</td>\n",
       "      <td>5.69041</td>\n",
       "      <td>7.14258</td>\n",
       "      <td>5.36</td>\n",
       "      <td>-0.31</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603999.SH</th>\n",
       "      <td>4.89</td>\n",
       "      <td>4.8828</td>\n",
       "      <td>5.03196</td>\n",
       "      <td>6.03575</td>\n",
       "      <td>4.79</td>\n",
       "      <td>-0.32</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2855 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             收盘价     5周均线    20周均线   52周最高价   52周最低价  相对强弱   满足条件\n",
       "ts_code                                                          \n",
       "000001.SZ  18.01  18.1428  19.8788    25.01    17.69 -0.44  False\n",
       "000002.SZ   20.9  20.9335  22.3858    27.18    20.16 -0.42  False\n",
       "000004.SZ   22.7  22.3045  22.1178    25.97    15.09   0.3  False\n",
       "000006.SZ   4.46  4.47137  4.65279   5.1178     4.42 -0.34  False\n",
       "000008.SZ   2.07  2.07017  2.13248     2.29     2.03 -0.28  False\n",
       "...          ...      ...      ...      ...      ...   ...    ...\n",
       "603991.SH  46.22  45.2895  45.9498    56.33    42.52  -0.3  False\n",
       "603993.SH   6.85   6.8712  6.60969     7.29  5.02337 -0.06  False\n",
       "603997.SH   9.35  9.12559  9.00023     9.64     8.01 -0.08  False\n",
       "603998.SH   5.53  5.50385  5.69041  7.14258     5.36 -0.31  False\n",
       "603999.SH   4.89   4.8828  5.03196  6.03575     4.79 -0.32  False\n",
       "\n",
       "[2855 rows x 7 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd=df.apply(lambda x: select_stock2(x,0.2)).T\n",
    "dd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-03T06:23:10.534031Z",
     "start_time": "2021-08-03T06:23:10.508025Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘价</th>\n",
       "      <th>5周均线</th>\n",
       "      <th>20周均线</th>\n",
       "      <th>52周最高价</th>\n",
       "      <th>52周最低价</th>\n",
       "      <th>相对强弱</th>\n",
       "      <th>满足条件</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>300343.SZ</th>\n",
       "      <td>14.88</td>\n",
       "      <td>13.1147</td>\n",
       "      <td>9.47984</td>\n",
       "      <td>13.76</td>\n",
       "      <td>2.86</td>\n",
       "      <td>3.86</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300655.SZ</th>\n",
       "      <td>64.1</td>\n",
       "      <td>56.8269</td>\n",
       "      <td>45.0432</td>\n",
       "      <td>60.39</td>\n",
       "      <td>20.49</td>\n",
       "      <td>1.76</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300432.SZ</th>\n",
       "      <td>30</td>\n",
       "      <td>29.6701</td>\n",
       "      <td>25.1644</td>\n",
       "      <td>32.83</td>\n",
       "      <td>10.41</td>\n",
       "      <td>1.74</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300346.SZ</th>\n",
       "      <td>80</td>\n",
       "      <td>70.431</td>\n",
       "      <td>52.4345</td>\n",
       "      <td>82.28</td>\n",
       "      <td>26.9617</td>\n",
       "      <td>1.65</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603396.SH</th>\n",
       "      <td>96.89</td>\n",
       "      <td>96.0037</td>\n",
       "      <td>80.8438</td>\n",
       "      <td>101.86</td>\n",
       "      <td>33.48</td>\n",
       "      <td>1.55</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300712.SZ</th>\n",
       "      <td>106.05</td>\n",
       "      <td>98.4187</td>\n",
       "      <td>78.3307</td>\n",
       "      <td>107.66</td>\n",
       "      <td>38.2922</td>\n",
       "      <td>1.48</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300382.SZ</th>\n",
       "      <td>25.65</td>\n",
       "      <td>24.2082</td>\n",
       "      <td>20.0866</td>\n",
       "      <td>25.67</td>\n",
       "      <td>9.19693</td>\n",
       "      <td>1.45</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300631.SZ</th>\n",
       "      <td>53.45</td>\n",
       "      <td>47.4673</td>\n",
       "      <td>42.2878</td>\n",
       "      <td>55.8</td>\n",
       "      <td>20.22</td>\n",
       "      <td>1.38</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300681.SZ</th>\n",
       "      <td>76.19</td>\n",
       "      <td>69.8352</td>\n",
       "      <td>52.1967</td>\n",
       "      <td>75</td>\n",
       "      <td>30.7097</td>\n",
       "      <td>1.38</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002455.SZ</th>\n",
       "      <td>18.06</td>\n",
       "      <td>15.4781</td>\n",
       "      <td>11.8426</td>\n",
       "      <td>16.42</td>\n",
       "      <td>6.61</td>\n",
       "      <td>1.37</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002518.SZ</th>\n",
       "      <td>29.78</td>\n",
       "      <td>26.9729</td>\n",
       "      <td>22.2395</td>\n",
       "      <td>28.24</td>\n",
       "      <td>12.34</td>\n",
       "      <td>1.25</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000762.SZ</th>\n",
       "      <td>51.73</td>\n",
       "      <td>48.5346</td>\n",
       "      <td>41.3313</td>\n",
       "      <td>50.74</td>\n",
       "      <td>20.63</td>\n",
       "      <td>1.23</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300581.SZ</th>\n",
       "      <td>41.83</td>\n",
       "      <td>40.9627</td>\n",
       "      <td>39.0589</td>\n",
       "      <td>43.96</td>\n",
       "      <td>16.67</td>\n",
       "      <td>1.18</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600111.SH</th>\n",
       "      <td>48.8</td>\n",
       "      <td>44.9929</td>\n",
       "      <td>37.0869</td>\n",
       "      <td>47.7</td>\n",
       "      <td>19.0241</td>\n",
       "      <td>1.13</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002760.SZ</th>\n",
       "      <td>41.6</td>\n",
       "      <td>40.0058</td>\n",
       "      <td>35.0387</td>\n",
       "      <td>40.64</td>\n",
       "      <td>18.0243</td>\n",
       "      <td>1.12</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300693.SZ</th>\n",
       "      <td>41.18</td>\n",
       "      <td>33.4842</td>\n",
       "      <td>25.3597</td>\n",
       "      <td>35.99</td>\n",
       "      <td>16.79</td>\n",
       "      <td>1.11</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603690.SH</th>\n",
       "      <td>65.9</td>\n",
       "      <td>63.9854</td>\n",
       "      <td>54.9401</td>\n",
       "      <td>69.13</td>\n",
       "      <td>30.7886</td>\n",
       "      <td>1.1</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002326.SZ</th>\n",
       "      <td>24.89</td>\n",
       "      <td>23.1731</td>\n",
       "      <td>22.4271</td>\n",
       "      <td>25.99</td>\n",
       "      <td>10.47</td>\n",
       "      <td>1.1</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002371.SZ</th>\n",
       "      <td>402.6</td>\n",
       "      <td>397.219</td>\n",
       "      <td>350.779</td>\n",
       "      <td>432</td>\n",
       "      <td>168.839</td>\n",
       "      <td>1.09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300480.SZ</th>\n",
       "      <td>43.32</td>\n",
       "      <td>42.9202</td>\n",
       "      <td>36.6993</td>\n",
       "      <td>45.62</td>\n",
       "      <td>18.64</td>\n",
       "      <td>1.09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              收盘价     5周均线    20周均线  52周最高价   52周最低价  相对强弱  满足条件\n",
       "ts_code                                                         \n",
       "300343.SZ   14.88  13.1147  9.47984   13.76     2.86  3.86  True\n",
       "300655.SZ    64.1  56.8269  45.0432   60.39    20.49  1.76  True\n",
       "300432.SZ      30  29.6701  25.1644   32.83    10.41  1.74  True\n",
       "300346.SZ      80   70.431  52.4345   82.28  26.9617  1.65  True\n",
       "603396.SH   96.89  96.0037  80.8438  101.86    33.48  1.55  True\n",
       "300712.SZ  106.05  98.4187  78.3307  107.66  38.2922  1.48  True\n",
       "300382.SZ   25.65  24.2082  20.0866   25.67  9.19693  1.45  True\n",
       "300631.SZ   53.45  47.4673  42.2878    55.8    20.22  1.38  True\n",
       "300681.SZ   76.19  69.8352  52.1967      75  30.7097  1.38  True\n",
       "002455.SZ   18.06  15.4781  11.8426   16.42     6.61  1.37  True\n",
       "002518.SZ   29.78  26.9729  22.2395   28.24    12.34  1.25  True\n",
       "000762.SZ   51.73  48.5346  41.3313   50.74    20.63  1.23  True\n",
       "300581.SZ   41.83  40.9627  39.0589   43.96    16.67  1.18  True\n",
       "600111.SH    48.8  44.9929  37.0869    47.7  19.0241  1.13  True\n",
       "002760.SZ    41.6  40.0058  35.0387   40.64  18.0243  1.12  True\n",
       "300693.SZ   41.18  33.4842  25.3597   35.99    16.79  1.11  True\n",
       "603690.SH    65.9  63.9854  54.9401   69.13  30.7886   1.1  True\n",
       "002326.SZ   24.89  23.1731  22.4271   25.99    10.47   1.1  True\n",
       "002371.SZ   402.6  397.219  350.779     432  168.839  1.09  True\n",
       "300480.SZ   43.32  42.9202  36.6993   45.62    18.64  1.09  True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd.query('满足条件==True').sort_values('相对强弱',ascending=False)[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-26T08:54:52.850763Z",
     "start_time": "2021-05-26T08:54:50.184731Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘价</th>\n",
       "      <th>50日均线</th>\n",
       "      <th>150日均线</th>\n",
       "      <th>200日均线</th>\n",
       "      <th>52周最高价</th>\n",
       "      <th>52周最低价</th>\n",
       "      <th>相对强弱</th>\n",
       "      <th>满足条件</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000001.SZ</th>\n",
       "      <td>24.6</td>\n",
       "      <td>23.2885</td>\n",
       "      <td>21.0905</td>\n",
       "      <td>19.3903</td>\n",
       "      <td>24.7554</td>\n",
       "      <td>12.5017</td>\n",
       "      <td>0.7</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.SZ</th>\n",
       "      <td>26.76</td>\n",
       "      <td>27.3996</td>\n",
       "      <td>28.9178</td>\n",
       "      <td>28.5043</td>\n",
       "      <td>33.5</td>\n",
       "      <td>24.2626</td>\n",
       "      <td>-0.11</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000004.SZ</th>\n",
       "      <td>15.63</td>\n",
       "      <td>15.9259</td>\n",
       "      <td>19.3111</td>\n",
       "      <td>21.7607</td>\n",
       "      <td>34.1</td>\n",
       "      <td>15.07</td>\n",
       "      <td>-0.73</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.SZ</th>\n",
       "      <td>5.26</td>\n",
       "      <td>5.34928</td>\n",
       "      <td>5.41151</td>\n",
       "      <td>5.51938</td>\n",
       "      <td>9.13</td>\n",
       "      <td>4.54142</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000008.SZ</th>\n",
       "      <td>2.28</td>\n",
       "      <td>2.27824</td>\n",
       "      <td>2.44375</td>\n",
       "      <td>2.59947</td>\n",
       "      <td>3.3</td>\n",
       "      <td>2.17</td>\n",
       "      <td>-0.45</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603991.SH</th>\n",
       "      <td>51.08</td>\n",
       "      <td>52.2205</td>\n",
       "      <td>44.4976</td>\n",
       "      <td>40.8445</td>\n",
       "      <td>59.51</td>\n",
       "      <td>22.13</td>\n",
       "      <td>0.64</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603993.SH</th>\n",
       "      <td>5.68</td>\n",
       "      <td>5.84307</td>\n",
       "      <td>5.62632</td>\n",
       "      <td>5.26639</td>\n",
       "      <td>7.6</td>\n",
       "      <td>3.3135</td>\n",
       "      <td>0.45</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603997.SH</th>\n",
       "      <td>8.67</td>\n",
       "      <td>8.46713</td>\n",
       "      <td>8.2259</td>\n",
       "      <td>8.03632</td>\n",
       "      <td>9.45</td>\n",
       "      <td>6.63</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603998.SH</th>\n",
       "      <td>7.21</td>\n",
       "      <td>6.32907</td>\n",
       "      <td>5.86112</td>\n",
       "      <td>6.01037</td>\n",
       "      <td>7.40135</td>\n",
       "      <td>4.82</td>\n",
       "      <td>-0.05</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603999.SH</th>\n",
       "      <td>5.92</td>\n",
       "      <td>5.56928</td>\n",
       "      <td>5.43269</td>\n",
       "      <td>5.57442</td>\n",
       "      <td>6.89</td>\n",
       "      <td>4.76</td>\n",
       "      <td>-0.18</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2836 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             收盘价    50日均线   150日均线   200日均线   52周最高价   52周最低价  相对强弱   满足条件\n",
       "ts_code                                                                   \n",
       "000001.SZ   24.6  23.2885  21.0905  19.3903  24.7554  12.5017   0.7   True\n",
       "000002.SZ  26.76  27.3996  28.9178  28.5043     33.5  24.2626 -0.11  False\n",
       "000004.SZ  15.63  15.9259  19.3111  21.7607     34.1    15.07 -0.73  False\n",
       "000006.SZ   5.26  5.34928  5.41151  5.51938     9.13  4.54142  -0.1  False\n",
       "000008.SZ   2.28  2.27824  2.44375  2.59947      3.3     2.17 -0.45  False\n",
       "...          ...      ...      ...      ...      ...      ...   ...    ...\n",
       "603991.SH  51.08  52.2205  44.4976  40.8445    59.51    22.13  0.64  False\n",
       "603993.SH   5.68  5.84307  5.62632  5.26639      7.6   3.3135  0.45  False\n",
       "603997.SH   8.67  8.46713   8.2259  8.03632     9.45     6.63 -0.01  False\n",
       "603998.SH   7.21  6.32907  5.86112  6.01037  7.40135     4.82 -0.05  False\n",
       "603999.SH   5.92  5.56928  5.43269  5.57442     6.89     4.76 -0.18  False\n",
       "\n",
       "[2836 rows x 8 columns]"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd=df.apply(lambda x: select_stock(x,0.2)).T\n",
    "dd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-26T07:14:44.725399Z",
     "start_time": "2021-05-26T07:14:44.695381Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘价</th>\n",
       "      <th>50日均线</th>\n",
       "      <th>150日均线</th>\n",
       "      <th>200日均线</th>\n",
       "      <th>52周最高价</th>\n",
       "      <th>52周最低价</th>\n",
       "      <th>相对强弱</th>\n",
       "      <th>满足条件</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>002612.SZ</th>\n",
       "      <td>61.44</td>\n",
       "      <td>53.7135</td>\n",
       "      <td>34.4648</td>\n",
       "      <td>27.456</td>\n",
       "      <td>58.76</td>\n",
       "      <td>6.93767</td>\n",
       "      <td>7.31</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300274.SZ</th>\n",
       "      <td>90.06</td>\n",
       "      <td>83.7552</td>\n",
       "      <td>72.258</td>\n",
       "      <td>60.0245</td>\n",
       "      <td>119.09</td>\n",
       "      <td>9.94022</td>\n",
       "      <td>7.27</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000799.SZ</th>\n",
       "      <td>238.5</td>\n",
       "      <td>213.583</td>\n",
       "      <td>163.422</td>\n",
       "      <td>137.741</td>\n",
       "      <td>238</td>\n",
       "      <td>31.6647</td>\n",
       "      <td>5.29</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>601919.SH</th>\n",
       "      <td>21.2</td>\n",
       "      <td>19.4907</td>\n",
       "      <td>13.8097</td>\n",
       "      <td>11.4044</td>\n",
       "      <td>22.29</td>\n",
       "      <td>3.14</td>\n",
       "      <td>5.13</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002709.SZ</th>\n",
       "      <td>77.01</td>\n",
       "      <td>68.0476</td>\n",
       "      <td>54.5611</td>\n",
       "      <td>46.4798</td>\n",
       "      <td>77.55</td>\n",
       "      <td>14.7914</td>\n",
       "      <td>3.52</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000516.SZ</th>\n",
       "      <td>18.7</td>\n",
       "      <td>16.993</td>\n",
       "      <td>12.7639</td>\n",
       "      <td>10.7999</td>\n",
       "      <td>18.71</td>\n",
       "      <td>3.97</td>\n",
       "      <td>3.47</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600744.SH</th>\n",
       "      <td>9.24</td>\n",
       "      <td>6.9989</td>\n",
       "      <td>4.78925</td>\n",
       "      <td>4.08369</td>\n",
       "      <td>9.12</td>\n",
       "      <td>2.17</td>\n",
       "      <td>3</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300171.SZ</th>\n",
       "      <td>37.83</td>\n",
       "      <td>32.4372</td>\n",
       "      <td>22.7346</td>\n",
       "      <td>20.2261</td>\n",
       "      <td>36.88</td>\n",
       "      <td>8.91432</td>\n",
       "      <td>2.86</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002810.SZ</th>\n",
       "      <td>84.88</td>\n",
       "      <td>74.4655</td>\n",
       "      <td>57.6097</td>\n",
       "      <td>49.851</td>\n",
       "      <td>82.9</td>\n",
       "      <td>17.3251</td>\n",
       "      <td>2.77</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600809.SH</th>\n",
       "      <td>458.65</td>\n",
       "      <td>423.093</td>\n",
       "      <td>346.101</td>\n",
       "      <td>297.96</td>\n",
       "      <td>456</td>\n",
       "      <td>102.788</td>\n",
       "      <td>2.76</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>601888.SH</th>\n",
       "      <td>339.28</td>\n",
       "      <td>312.867</td>\n",
       "      <td>283.61</td>\n",
       "      <td>251.781</td>\n",
       "      <td>387.86</td>\n",
       "      <td>85.8535</td>\n",
       "      <td>2.63</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603877.SH</th>\n",
       "      <td>50.42</td>\n",
       "      <td>48.8609</td>\n",
       "      <td>40.2769</td>\n",
       "      <td>34.3985</td>\n",
       "      <td>55.1012</td>\n",
       "      <td>13.1418</td>\n",
       "      <td>2.61</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600499.SH</th>\n",
       "      <td>15.2</td>\n",
       "      <td>13.9269</td>\n",
       "      <td>9.73833</td>\n",
       "      <td>8.30463</td>\n",
       "      <td>16.03</td>\n",
       "      <td>3.92</td>\n",
       "      <td>2.52</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603128.SH</th>\n",
       "      <td>15.92</td>\n",
       "      <td>15.1966</td>\n",
       "      <td>12.3436</td>\n",
       "      <td>10.6037</td>\n",
       "      <td>15.95</td>\n",
       "      <td>4.10238</td>\n",
       "      <td>2.49</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002444.SZ</th>\n",
       "      <td>36.56</td>\n",
       "      <td>36.3129</td>\n",
       "      <td>32.5365</td>\n",
       "      <td>28.375</td>\n",
       "      <td>40.8</td>\n",
       "      <td>9.32</td>\n",
       "      <td>2.45</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000762.SZ</th>\n",
       "      <td>24.95</td>\n",
       "      <td>19.5336</td>\n",
       "      <td>15.2829</td>\n",
       "      <td>13.5312</td>\n",
       "      <td>23.66</td>\n",
       "      <td>6.7</td>\n",
       "      <td>2.4</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002240.SZ</th>\n",
       "      <td>29.48</td>\n",
       "      <td>26.3428</td>\n",
       "      <td>22.3582</td>\n",
       "      <td>19.9123</td>\n",
       "      <td>30.14</td>\n",
       "      <td>7.59</td>\n",
       "      <td>2.36</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600753.SH</th>\n",
       "      <td>32.1</td>\n",
       "      <td>26.2506</td>\n",
       "      <td>20.8968</td>\n",
       "      <td>18.66</td>\n",
       "      <td>36.37</td>\n",
       "      <td>8.93685</td>\n",
       "      <td>2.34</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603906.SH</th>\n",
       "      <td>23.7</td>\n",
       "      <td>21.8064</td>\n",
       "      <td>20.434</td>\n",
       "      <td>18.5024</td>\n",
       "      <td>24.3543</td>\n",
       "      <td>6.06823</td>\n",
       "      <td>2.32</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>601015.SH</th>\n",
       "      <td>8.75</td>\n",
       "      <td>8.08136</td>\n",
       "      <td>6.63227</td>\n",
       "      <td>5.70409</td>\n",
       "      <td>11.0295</td>\n",
       "      <td>2.49658</td>\n",
       "      <td>2.24</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              收盘价    50日均线   150日均线   200日均线   52周最高价   52周最低价  相对强弱  满足条件\n",
       "ts_code                                                                   \n",
       "002612.SZ   61.44  53.7135  34.4648   27.456    58.76  6.93767  7.31  True\n",
       "300274.SZ   90.06  83.7552   72.258  60.0245   119.09  9.94022  7.27  True\n",
       "000799.SZ   238.5  213.583  163.422  137.741      238  31.6647  5.29  True\n",
       "601919.SH    21.2  19.4907  13.8097  11.4044    22.29     3.14  5.13  True\n",
       "002709.SZ   77.01  68.0476  54.5611  46.4798    77.55  14.7914  3.52  True\n",
       "000516.SZ    18.7   16.993  12.7639  10.7999    18.71     3.97  3.47  True\n",
       "600744.SH    9.24   6.9989  4.78925  4.08369     9.12     2.17     3  True\n",
       "300171.SZ   37.83  32.4372  22.7346  20.2261    36.88  8.91432  2.86  True\n",
       "002810.SZ   84.88  74.4655  57.6097   49.851     82.9  17.3251  2.77  True\n",
       "600809.SH  458.65  423.093  346.101   297.96      456  102.788  2.76  True\n",
       "601888.SH  339.28  312.867   283.61  251.781   387.86  85.8535  2.63  True\n",
       "603877.SH   50.42  48.8609  40.2769  34.3985  55.1012  13.1418  2.61  True\n",
       "600499.SH    15.2  13.9269  9.73833  8.30463    16.03     3.92  2.52  True\n",
       "603128.SH   15.92  15.1966  12.3436  10.6037    15.95  4.10238  2.49  True\n",
       "002444.SZ   36.56  36.3129  32.5365   28.375     40.8     9.32  2.45  True\n",
       "000762.SZ   24.95  19.5336  15.2829  13.5312    23.66      6.7   2.4  True\n",
       "002240.SZ   29.48  26.3428  22.3582  19.9123    30.14     7.59  2.36  True\n",
       "600753.SH    32.1  26.2506  20.8968    18.66    36.37  8.93685  2.34  True\n",
       "603906.SH    23.7  21.8064   20.434  18.5024  24.3543  6.06823  2.32  True\n",
       "601015.SH    8.75  8.08136  6.63227  5.70409  11.0295  2.49658  2.24  True"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd.query('满足条件==True').sort_values('相对强弱',ascending=False)[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
